# -*- coding: utf-8 -*-


from dataclasses import field
from datetime import date
from typing import Optional
from marshmallow.validate import Length, Range
from marshmallow_dataclass import dataclass

from index.protocol import Field


class _BookFiledValidate(object):
    title = Length(min=1, max=100, error="标题长度要在{min}到{max}个字符")
    price = Range(min=0.01, max=999999999, error="价格要在{min}到{max}之间")
    describe = Length(max=255, error="书籍描述不能超过{max}个字符")
    pub = Length(min=2, max=100, error="出版社名称要在{min}到{max}个字符之间")


@dataclass
class Book:
    title: str = field(metadata=Field(validate=_BookFiledValidate.title))
    price: float = field(metadata=Field(validate=_BookFiledValidate.price))
    retail_price: float = field(metadata=Field(validate=_BookFiledValidate.price))
    pub: str = field(metadata=Field(validate=_BookFiledValidate.pub))
    authors: Optional[str]
    cover_uri: Optional[str] = field(default='')
    pub_date: Optional[date] = field(default=date.today())
    describe: Optional[str] = field(metadata=Field(validate=_BookFiledValidate.describe), default='')



@dataclass
class BookUpdateInfo:
    id: int
    title: Optional[str] = field(metadata=Field(validate=_BookFiledValidate.title))
    price: Optional[float] = field(metadata=Field(validate=_BookFiledValidate.price))
    retail_price: Optional[float] = field(metadata=Field(validate=_BookFiledValidate.price))

    describe: Optional[str] = field(metadata=Field(validate=_BookFiledValidate.describe))
    pub: Optional[str]
    authors: Optional[str]
    cover_uri: Optional[str]
    pub_date: Optional[date]
